package com.joker.airoleplayplatformback.controller;

import com.joker.airoleplayplatformback.domain.entity.R;
import com.joker.airoleplayplatformback.domain.po.ChatMessagePO;
import com.joker.airoleplayplatformback.domain.po.ChatSessionPO;
import com.joker.airoleplayplatformback.service.ChatService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 聊天控制器
 * @author feixiang.li
 * @since 2025-09-27
 */
@Slf4j
@RestController
@RequestMapping("/api/chat")
@Tag(name = "聊天管理", description = "聊天相关接口")
public class ChatController {

    @Autowired
    private ChatService chatService;

    @PostMapping("/session/create")
    @Operation(summary = "创建新的聊天会话")
    public R<ChatSessionPO> createSession(@RequestParam Long userId, @RequestParam Long agentId) {
        try {
            ChatSessionPO session = chatService.createNewSession(userId, agentId);
            return R.ok(session);
        } catch (Exception e) {
            log.error("创建聊天会话失败", e);
            return R.error(500,"创建聊天会话失败: " + e.getMessage());
        }
    }

    @GetMapping("/session/list")
    @Operation(summary = "获取用户的所有会话")
    public R<List<ChatSessionPO>> getUserSessions(@RequestParam String userId) {
        try {
            List<ChatSessionPO> sessions = chatService.getUserSessions(userId);
            return R.ok(sessions);
        } catch (Exception e) {
            log.error("获取用户会话列表失败", e);
            return R.error(500,"获取用户会话列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/session/{sessionId}")
    @Operation(summary = "获取会话详情")
    public R<ChatSessionPO> getSession(@PathVariable String sessionId) {
        try {
            ChatSessionPO session = chatService.getSession(sessionId);
            if (session == null) {
                return R.error(500,"会话不存在");
            }
            return R.ok(session);
        } catch (Exception e) {
            log.error("获取会话详情失败", e);
            return R.error(500,"获取会话详情失败: " + e.getMessage());
        }
    }

    @GetMapping("/messages/{sessionId}")
    @Operation(summary = "获取会话的所有消息")
    public R<List<ChatMessagePO>> getSessionMessages(@PathVariable String sessionId) {
        try {
            List<ChatMessagePO> messages = chatService.getSessionMessages(sessionId);
            return R.ok(messages);
        } catch (Exception e) {
            log.error("获取会话消息失败", e);
            return R.error(500,"获取会话消息失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/session/{sessionId}")
    @Operation(summary = "删除会话")
    public R<Boolean> deleteSession(@PathVariable String sessionId) {
        try {
            boolean result = chatService.deleteSession(sessionId);
            return R.ok(result);
        } catch (Exception e) {
            log.error("删除会话失败", e);
            return R.error(500,"删除会话失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/message/{messageId}")
    @Operation(summary = "删除消息")
    public R<Boolean> deleteMessage(@PathVariable String messageId) {
        try {
            boolean result = chatService.deleteMessage(messageId);
            return R.ok(result);
        } catch (Exception e) {
            log.error("删除消息失败", e);
            return R.error(500,"删除消息失败: " + e.getMessage());
        }
    }
}
